// 引入接口文件
import { getProjectPermission,findByCity,addFun,editFun,deleteFun} from './countySetServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import Cookie from 'js-cookie'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
// 导入项目全局配置文件
import config from 'config/config'
// 初始默认状态
const defultState = {
  biz: {
	queryForm: {}
  },
  areaList: [],
  cityList: [],
  modalObj:{
    visible:false,
    type:"",
  },
  detail:{
	"area": "",
	"areaId": "",
	"city": "",
	"cityId": "",
	"createBy": "",
  "createTime": "",
  "cityStatus": "1",
	"id": "",
	"region": "",
	// "regionId": ""
    },
}

// 声明module
const tmpModule = {
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
            if(query.actionType == 'check'|| query.actionType == 'edit'){
                dispatch({type: 'queryInfo',payload: {id:query.id}})
              }
              dispatch({type: 'getProjectPermission',payload: {}})
        } // end if
      }) // end listen
    },
  }, 

  // 方法列表
  effects: {
    *getProjectPermission({payload}, {put, select, call}){
        let {areaList} = yield select(state => state[tmpModule.namespace])
        let params = { 
          userId: localStorage.username
        }
        const { success,projectList} = yield call(getProjectPermission, {...params})
        if(success){
        areaList =  projectList.map((item,index)=>{
                      return {
                        label: item.areaName,
                        value: item.areaId,
                      }
                    })
          yield put({
            type: 'updateStore',
            payload: {areaList}
          })
        }
    },
    *findByCity({payload}, {put, select, call}){
      let {cityList} = yield select(state => state[tmpModule.namespace])
      let params = { 
        area: payload.areaId,
      }
      const { success,result} = yield call(findByCity, {...params})
      if(success){
        cityList = result.filter(item => !!item)
        cityList = cityList.map((itemed,index)=>{
            return {
              label: itemed.city,
              value: itemed.cityid,
            }
          })
        yield put({
          type: 'updateStore',
          payload: {cityList}
        })
      }
    },
    *addOrEditDetail({ payload }, { put, call, select }){
        try{
          var {detail,modalObj,biz} = yield select(state => state[tmpModule.namespace])
          const userName = Cookie.get(config.cookie.userName);
          let serv = modalObj.type=='add'?addFun:editFun;
        
          detail.createBy = userName;
          const {success,data} = yield call(serv,detail)
          if(success){
            message.success(`${modalObj.type == 'edit'?'编辑':'新增'}成功！`);
            modalObj.visible = !modalObj.visible;
            modalObj.type = "";
            payload.resetFields()
            yield put({ type: 'updateStore', payload: {modalObj,detail:{}} });
            biz.refreshListDatas(true)
          }
        }catch(e){console.log(e)}
      },
      *deleteFun({ payload }, { put, call, select }){
        try{
          var {biz} = yield select(state => state[tmpModule.namespace])
          let params = { 
            id: payload.id,
          }
          const {success,data} = yield call(deleteFun,params)
          if(success){
            message.success(`删除成功`);
            biz.refreshListDatas(true)
          }
        }catch(e){console.log(e)}
      },
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
